package com.inifitness.common.rabbitmq;

import java.util.HashMap;
import java.util.Map;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;

/**
 * 自定义Rabbit设置
 *
 * @author sunjinfeng
 * @version 1.0.0
 * @since 2023-02-02 13:10
 */
//@Configuration
public class CustomRabbitConfig {

  public static final String DELAYED_SMS_QUEUE = "inifitness.custom.delay.sms.queue";
  public static final String DELAYED_MAIL_QUEUE = "inifitness.custom.delay.mail.queue";
  public static final String DELAYED_EXCHANGE = "inifitness.custom.delay.exchange";
  public static final String DELAYED_SMS_ROUTING_KEY = "inifitness.custom.delay.sms.key";
  public static final String DELAYED_MAIL_ROUTING_KEY = "inifitness.custom.delay.mail.key";

  @Bean
  public Queue delayedSmsQueue() {
    return new Queue(DELAYED_SMS_QUEUE);
  }

  @Bean
  public Queue delayedMailQueue() {
    return new Queue(DELAYED_MAIL_QUEUE);
  }

  @Bean
  public CustomExchange customExchange() {
    Map<String, Object> args = new HashMap<>();
    args.put("x-delayed-type", "direct");
    return new CustomExchange(DELAYED_EXCHANGE, "x-delayed-message", true, false, args);
  }

  @Bean
  public Binding bindingSmsNotify(@Qualifier("delayedSmsQueue") Queue delayedSmsQueue,
      CustomExchange customExchange) {
    return BindingBuilder.bind(delayedSmsQueue).to(customExchange).with(DELAYED_SMS_ROUTING_KEY)
        .noargs();
  }

  @Bean
  public Binding bindingMailNotify(Queue delayedMailQueue,
      CustomExchange customExchange) {
    return BindingBuilder.bind(delayedMailQueue).to(customExchange).with(DELAYED_MAIL_ROUTING_KEY)
        .noargs();
  }
}
